package com.wdz.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.wdz.admin.entity.Admin;
import com.wdz.http.Response;
import com.wdz.service.IAdminService;
import com.wdz.service.IApiUserService;
import com.wdz.utils.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class AdminController {

    @Autowired
    private IApiUserService iApiUserService;
    @Autowired
    private IAdminService iAdminService;


    @PostMapping("/getApiUser")
    public Response getApiUser(@RequestParam String username){
        return Response.success(iApiUserService.queryApiUser(username));
    }

    @PostMapping("/getUserInfo")
    public Response getUserInfo(@RequestParam String username){
        return Response.success(iAdminService.queryAdmin(username));
    }
    @Autowired
    private com.wdz.admin.service.IAdminService iAdminService2;

    @PostMapping("/checkUser")
    public Response checkUser(@RequestParam String username,@RequestParam String password){
        Admin admin = iAdminService2.selectOne(new EntityWrapper<Admin>().where("admin_name={0}",username) );
        if(admin == null){
            return Response.error("用户名不存在");
        }
        if(MD5Util.makePwd(password,admin.getSalt()).equals(admin.getPassword())){
            return Response.success("用户校验成功");
        }
        return Response.error("密码错误");
    }
    /**
     * 校验密码是否正确
     * @return
     */
    @PostMapping("/checkPassword")
    public Response checkPassword(@RequestParam String password,@RequestParam String username){
        Admin admin = iAdminService2.selectOne(new EntityWrapper<Admin>().where("admin_name={0}",username) );
        if(MD5Util.makePwd(password,admin.getSalt()).equals(admin.getPassword())){
            return Response.success("密码正确",true);
        }
        return Response.success("密码错误",false);
    }
    /**
     * 修改密码
     */
    @PostMapping("/updatePassword")
    public Response updatePassword(@RequestParam String newPassword,@RequestParam String username){
            Admin admin = iAdminService2.selectOne(new EntityWrapper<Admin>().where("admin_name={0}", username));
            iAdminService2.updateForSet("password='" + MD5Util.makePwd(newPassword, admin.getSalt())+"'", new EntityWrapper<Admin>().where("admin_name={0}", username));
            return Response.success("密码修改成功");
    }
}
